package co.chuangdian.oflauncher.setting;

import java.util.ArrayList;

import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import cn.onekit.android.ACTIVITY;

@SuppressLint("ShowToast")
public class FeedbackActivity extends OFActivity implements OnItemClickListener {
	MyAdapter mAdapter;
	ListView mListView;
	ImageButton bt;
	EditText ed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_feedback);
		//
		mListView = (ListView) findViewById(R.id.setting_feedbacklistview);

		ArrayList<String> data = new ArrayList<String>();
		data.add(getResources().getString(R.string.setting_feedback_problem));
		data.add(getResources().getString(R.string.setting_feedback_suggest));
		data.add(getResources().getString(R.string.setting_feedback_doubt));
		//
		mAdapter = new MyAdapter(this, data);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		//
		ed = (EditText) findViewById(R.id.setting_feedback_edittext);
		//
		bt = (ImageButton) findViewById(R.id.feedback_ImageButton);
		bt.getBackground().setAlpha(0);
		bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(ed.getText().toString())) {
					Toast.makeText(FeedbackActivity.this,
							"请输入反馈内容", 1000).show();
				} else if (mAdapter.choiceID == -1) {
					Toast.makeText(FeedbackActivity.this,
							"请选择反馈类型", 1000).show();
				} else {
					Intent intent = new Intent();
					intent.setClass(FeedbackActivity.this,
							FeedbacksuccessActivity.class);
					startActivity(intent);
				}
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
       ed.setText("");
	}

	//
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		mAdapter.setChoiceId(position);
	}

	//
	class MyAdapter extends BaseAdapter {
		private LayoutInflater minflater;
		ArrayList<String> data;
		private int choiceID = -1;

		public MyAdapter(Context context, ArrayList<String> data) {
			this.data = data;
			this.minflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public void setChoiceId(int id) {
			this.choiceID = id;
			this.notifyDataSetChanged();
		}

		// 返回选中item的下标,如果返回-1 表示没有任何选择
		public int getChoiceId() {
			return this.choiceID;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = minflater.inflate(R.layout.setting_feedback_view,
						parent, false);
				holder.nameView = (TextView) convertView
						.findViewById(R.id.setting_feedback_textview);
				holder.rb = (RadioButton) convertView
						.findViewById(R.id.setting_feedback_radio);
				convertView.setTag(holder);// 在View里面有一个Tag属性保存Object对象
			} else {
				holder = (ViewHolder) convertView.getTag(); // 从View里面取出保存的对象
			}

			holder.nameView.setText(data.get(position));// 设定内容
			holder.rb.setChecked(position == choiceID); // 设定RadioButton的选中状态
			holder.rb.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// 当前选择的和之前选择的是同一个RadioButton，那么就不做任何的事情
					if (choiceID == position) {
						return;
					}
					choiceID = position;
					notifyDataSetChanged();
				}
			});
			return convertView;
		}
	}

	class ViewHolder {
		private TextView nameView;
		private RadioButton rb;
	}
}
